import tkinter as tk
from tkinter import messagebox
from link import *
from PIL import ImageTk, Image
from visitorPage import *
from administratePage import administratePage
import order_list as ol

class adminiLoginPage():
    def __init__(self, master=None):
        self.window = master
        self.window.title('管理员登录')
        self.window.geometry('750x750')
        self.name = tk.StringVar()
        self.pwd = tk.StringVar()
        self.create_page()

    def create_page(self):
        global image2
        self.page = tk.Frame(self.window, width=750, height=750)
        self.page.pack()
        img = Image.open('yotsyba.png')
        image2 = ImageTk.PhotoImage(img)
        label_img = tk.Label(self.page, image=image2)
        label_img.pack()

        tk.Label(self.window, text="账号", font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin').place(x=10, y=150, anchor='nw')
        tk.Entry(self.window, textvariable=self.name, font=('microsoft yahei', 20), bd=4).place(x=100, y=150, anchor='nw')

        tk.Label(self.window, text="密码", font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin').place(x=10, y=250, anchor='nw')
        tk.Entry(self.window, textvariable=self.pwd, font=('microsoft yahei', 20), show='*', bd=4).place(x=100, y=250, anchor='nw')

        tk.Button(self.window, text='登录', font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin', command=self.login).place(x=200, y=380, anchor='nw')
        self.window.mainloop()
     
    
    def login(self):
        obj = loginSearch()
        result = obj.get_info()
        username = self.name.get()
        pwd = self.pwd.get()

        flag = False

        for key, value in result.items():
            if str(key) == username and value == pwd:
                messagebox.showinfo(title='', message='登陆成功')
                flag = True
            elif str(key) == username and value != pwd:
                messagebox.showinfo(title='', message='密码错误')
            else:
                messagebox.showinfo(title='', message='您不是管理员')

        obj.close_conn()
        
        if flag:
            self.page.destroy()
            administratePage(self.window)


class visitorLoginPage():
    def __init__(self, master=None):
        self.window = master
        self.window.title('访客登录')
        self.window.geometry('750x750')
        self.name = tk.StringVar()
        self.pwd = tk.StringVar()
        self.create_page()

    def create_page(self):
        self.page = tk.Frame(self.window, width=750, height=750)
        self.page.pack()
        img = Image.open('teio.png')
        self.image = ImageTk.PhotoImage(img)
        label_img = tk.Label(self.page, image=self.image)
        label_img.pack()

        tk.Label(self.window, text="账号", font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin').place(x=10, y=150, anchor='nw')
        tk.Entry(self.window, textvariable=self.name, font=('microsoft yahei', 20), bd=4).place(x=100, y=150, anchor='nw')

        tk.Label(self.window, text="密码", font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin').place(x=10, y=250, anchor='nw')
        tk.Entry(self.window, textvariable=self.pwd, font=('microsoft yahei', 20), show='*', bd=4).place(x=100, y=250, anchor='nw')

        tk.Button(self.window, text='登录', font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin', command=self.login).place(x=200, y=380, anchor='nw')

    def login(self):
        obj = loginSearch()
        result = obj.get_info_v()
        username = self.name.get()
        pwd = self.pwd.get()

        flag = False

        for key, value in result.items():
            if str(key) == username and value == pwd:
                messagebox.showinfo(title='', message='登陆成功')
                flag = True
                break
            elif str(key) == username and value != pwd:
                messagebox.showinfo(title='', message='密码错误')
            else:
                messagebox.showinfo(title='', message='您还未注册')

        obj.close_conn()

        if flag:
            self.page.destroy()
            visitorPage(self.window, username)
